<template>
  <div class="app-container">
    <el-form 
      :model="queryParams" 
      ref="queryForm" 
      :inline="true" 
      label-width="68px" 
      @submit.native.prevent>
      <el-form-item label="" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入关键字"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button 
          type="primary" 
          icon="el-icon-search" 
          size="mini" 
          @click="handleQuery">搜索
        </el-button>
        <el-button 
          icon="el-icon-refresh" 
          size="mini" 
          @click="resetQuery">重置
        </el-button>
      </el-form-item>
    </el-form>
      <div :class="tableMain" v-show="tableMainShow">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="formAdd"
            >新增From</el-button>
          </el-col>
          <el-col :span="1.5" style="float: right;">
            <el-button 
              size="mini" 
              @click="tableExtend(1)" 
              icon="el-icon-full-screen">
            </el-button>
          </el-col>
        </el-row>
        <el-table 
          v-loading="loading" 
          :data="formList" 
          stripe 
          @selection-change="handleSelectionChange"  
          :header-cell-style="{'background': '#F6F8FB'}"
          @row-click="row=>checkColumn(row.id,row.name)" 
          :height="maxHeight"
          v-scrollbar="'.el-table__body-wrapper'">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="编码" align="center" prop="name" :show-overflow-tooltip="true" sortable/>
          <el-table-column label="描述" align="center" prop="description" :show-overflow-tooltip="true" />
          <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
          <el-table-column width="120" label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <div class="operate-group">
                <span
                  style="cursor:pointer;color: #409eff;font-size:12px;"
                  type="text"
                  class="el-icon-edit"
                  :plain=false
                  @click="formUpdate(scope.row.id)"
                >编辑</span>
                <span
                  style="cursor:pointer;color: #409eff;font-size:12px;"
                  type="text"
                  class="el-icon-delete"
                  :plain=false
                  @click="formDelete(scope.row.id)"
                >删除</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
      <resize-bar v-show="tableMainShow && tableDependShow"/>
      <div :class="tableDepend" v-show="tableDependShow">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="columnAdd"
            >新增列</el-button>
          </el-col>
          <el-col :span="1.5" style="float: right;">
            <el-button 
              size="mini" 
              @click="tableExtend(2)" 
              icon="el-icon-full-screen">
            </el-button>
          </el-col>
          <div class="tableDependTitle">{{ tableDependName }} 的详细列信息</div>
        </el-row>
        <el-table 
          v-loading="loading" 
          :data="formColumnList" 
          stripe 
          @selection-change="handleSelectionChange" 
          :header-cell-style="{'background': '#F6F8FB'}" 
          :height="maxHeight"
          v-scrollbar="'.el-table__body-wrapper'">
          <el-table-column label="名称" align="center" prop="name" />
          <el-table-column label="关联值" align="center" prop="prop" />
          <el-table-column label="标签类型" align="center" prop="componentType" :show-overflow-tooltip="true"/>
          <el-table-column label="显示排序" align="center" prop="sort" />
          <el-table-column width="120" label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <div class="operate-group">
                <span
                  style="cursor:pointer;color: #409eff;font-size:12px;"
                  type="text"
                  class="el-icon-edit"
                  :plain=false
                  @click="columnUpdate(scope.row.id)"
                >编辑</span>
                <span
                  style="cursor:pointer;color: #409eff;font-size:12px;"
                  type="text"
                  class="el-icon-delete"
                  :plain=false
                  @click="columnDelete(scope.row.id)"
                >删除</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="columnTotal>0"
          :total="columnTotal"
          :page.sync="queryColumnParams.pageNum"
          :limit.sync="queryColumnParams.pageSize"
          @pagination="getColumnList"
        />
      </div>

    <!-- 添加或修改from表单对话框 -->
    <el-dialog 
      :title="title" 
      :visible.sync="open" 
      width="500px" 
      append-to-body 
      v-dialogDrag 
      :close-on-click-modal="false">
      <el-form 
        ref="form" 
        :model="form" 
        :rules="rules" 
        label-width="80px" 
        @submit.native.prevent>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">正常</el-radio>
            <el-radio label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改form表单列对话框 -->
    <el-dialog 
      :title="title" 
      :visible.sync="columnOpen" 
      width="500px" 
      append-to-body 
      v-dialogDrag 
      :close-on-click-modal="false">
      <el-form 
        ref="columnForm" 
        :model="columnForm" 
        :rules="columnRules" 
        label-width="80px" 
        @submit.native.prevent>
        <el-form-item label="名称" prop="name">
          <el-input v-model="columnForm.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="columnForm.description" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="关联值" prop="prop">
          <el-input v-model="columnForm.prop" placeholder="请输入prop" />
        </el-form-item>
        <el-form-item label="标签类型" prop="componentType">
          <el-select v-model="columnForm.componentType" placeholder="标签类型" size="small">
            <el-option key="el-input" label="输入框" value="el-input"/>
            <el-option key="el-clear" label="行内清除" value="el-clear"/>
            <el-option key="el-input-auto" label="输入框(自动）" value="el-input-auto"/>
            <el-option key="el-textarea" label="文本域" value="el-textarea"/>
            <el-option key="el-select" label="选择框" value="el-select"/>
            <el-option key="el-selects" label="多选选框" value="el-selects"/>
            <el-option key="el-add-existing" label="添加现有项" value="el-add-existing"/>
            <el-option key="el-radio-group" label="单选" value="el-radio"/>
            <el-option key="el-checkbox-group" label="多选" value="el-checkbox-group"/>
            <el-option key="el-month-picker" label="年月" value="el-month-picker"/>
            <el-option key="el-date-picker" label="日期" value="el-date-picker"/>
            <el-option key="el-time-picker" label="时间" value="el-time-picker"/>
            <el-option key="el-dates-picker" label="多选日期" value="el-dates-picker"/>
            <el-option key="el-date-daterange" label="日期范围" value="el-date-daterange"/>
            <el-option key="el-file" label="文件" value="el-file"/>
            <el-option key="file" label="单文件" value="file"/>
            <el-option key="image" label="图片" value="image"/>
            <el-option key="el-cascader" label="级联选择器" value="el-cascader"/>
            <el-option key="el-rate" label="评分" value="el-rate"/>
          </el-select>
        </el-form-item>
        <el-form-item label="默认值" prop="defaultValue">
          <el-input v-model="columnForm.defaultValue" placeholder="请输入默认值" />
        </el-form-item>
        <el-form-item :label="columnForm.componentType=='el-textarea'?'字数控制':'选项值'" prop="option_value">
          <el-input v-model="columnForm.optionValue" placeholder="请输入选项值" />
        </el-form-item>
        <el-form-item label="显示/依赖" prop="show">
          <el-input v-model="columnForm.show" placeholder="请输入显示条件" />
        </el-form-item>
        <el-form-item label="显示排序" prop="sort">
          <el-input v-model="columnForm.sort" placeholder="请输入显示排序" />
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="是否必填" prop="required">
              <el-switch v-model="columnForm.required"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否并排" prop="abreast">
              <el-switch v-model="columnForm.abreast"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="可编辑" prop="editable">
              <el-switch v-model="columnForm.editable"></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="状态">
          <el-radio-group v-model="columnForm.status">
            <el-radio key="1" label="1">正常</el-radio>
            <el-radio key="0" label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="!columnForm.id" @click="submitColumnForm(true)">应 用</el-button>
        <el-button type="primary" @click="submitColumnForm">确 定</el-button>
        <el-button @click="cancelColumn">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { 
    listForm,
    listFormColumn,
    getForm,
    getColumn,
    addForm,
    updateForm,
    delForm,
    addColumnForm,
    updateColumnForm,
    delColumn 
  } from "@/api/pms/form";

  export default {
    inject: ['app'],
    name: "Form",
    components:{
      resizeBar: () => import("@/views/components/resizeBar"),
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        tableMain:"table-main",
        tableDepend:"table-depend",
        tableMainShow: true,
        tableDependShow: true,
        // 总条数
        total: 0,
        columnTotal:0,
        // 表格数据
        formList: [],
        formColumnList: [],
        tableDependName:"",
        formID:"",
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        columnOpen: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: this.$store.state.user.pageSize||30,
          name: undefined
        },
        queryColumnParams: {
          pageNum: 1,
          pageSize: this.$store.state.user.pageSize||30,
          formid: undefined
        },
        // 表单参数
        form: {status: '1'},
        // 表单校验
        rules: {
          tableName: [
            { required: true, message: "名称不能为空", trigger: "blur" }
          ],
        },
        columnForm: {sort:1,status:'1',componentType:"el-input",editable:true},
        // 表单校验
        columnRules: {
          name: [
            { required: true, message: "名称不能为空", trigger: "blur" }
          ],
          prop: [
            { required: true, message: "关联值不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    computed: {
      maxHeight(){
        return this.app.screenHeight - 270
      },
    },
    methods: {
      /** 查询参数列表 */
      getList() {
        this.loading = true;
        listForm(this.queryParams).then(response => {
            this.formList = response.rows;
            this.total = response.total;
            if (this.formList.length > 0) {
              this.tableDependName = response.rows[0].name;
              this.formID = response.rows[0].id;
              this.queryColumnParams.formid = response.rows[0].id;
              this.getColumnList();
            }else {
              this.formColumnList = [];
              this.columnTotal = 0;
            }
            this.loading = false;
          }
        );
      },
      getColumnList() {
        this.loading = true;
        listFormColumn(this.queryColumnParams).then(response => {
            this.formColumnList = response.rows;
            this.columnTotal = response.total;
            this.loading = false;
          }
        );
      },
      checkColumn(id, name) {
        this.tableDependName = name;
        this.formID = id;
        this.queryColumnParams.formid = id;
        this.getColumnList();
      },
      // 表单重置
      reset() {
        this.form = {
          name: undefined, status: "1"
        };
        this.resetForm("form");
        this.resetForm("columnForm");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      // 状态翻译
      statusFormat(row, column) {
        if (row.status == 1) {
          return "正常";
        }else{
          return "禁用";
        }
      },
      /** 新增按钮操作 */
      formAdd() {
        this.form = {
          status: "1"
        };
        this.open = true;
        this.title = "添加Form表单";
      },
      /** 修改按钮操作 */
      formUpdate(id) {
        this.reset();
        const Id = id || this.ids
        getForm(Id).then(response => {
          this.form = response.data;
          if(this.form.status==null){
            this.form.status = '1'
          }else{
            this.form.status = String(this.form.status)
          }
          this.open = true;
          this.title = "修改Form表单";
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateForm(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addForm(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      formDelete(Id) {
        const Ids = Id || this.ids;
        this.$confirm('是否确认删除Form表单编号为"' 
                      + (Array.isArray(Ids) && Ids.length > 2 ? (Ids[0] + '等') : Ids) 
                      + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delForm(Ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 新增列按钮操作 */
      columnAdd() {
        if (!this.formID) {
          this.msgError("请选择Form");
          return false;
        }
        this.columnForm = {sort:1,status:'1',componentType:"el-input",editable:true};
        this.columnOpen = true;
        this.title = "添加Form列";
      },
      /** 修改列按钮操作 */
      columnUpdate(lid) {
        this.reset();
        const Id = lid || this.ids
        getColumn(Id).then(response => {
          this.columnForm = response.data;
          if(this.columnForm.editable==null){
            this.columnForm.editable = true;
          }
          this.columnOpen = true;
          this.title = "修改Form列";
        });
      },
      // 列取消按钮
      cancelColumn() {
        this.columnOpen = false;
        this.reset();
      },
      /** 列提交按钮 */
      submitColumnForm: function(flag) {
        this.$refs["columnForm"].validate(valid => {
          if (valid) {
            if (this.columnForm.id != undefined) {
              this.columnForm.tableid = this.tableID;
              updateColumnForm(this.columnForm).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.resetForm("columnForm");
                  this.columnOpen = false;
                  this.getColumnList();
                }
              });
            } else {
              this.columnForm.formid = this.formID;
              addColumnForm(this.columnForm).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  if(!flag){
                    this.resetForm("columnForm");
                    this.columnOpen = false;
                    this.getColumnList();
                  }
                }
              });
            }
          }
        });
      },
      /** 删除列按钮操作 */
      columnDelete(Id) {
        const Ids = Id || this.ids;
        this.$confirm('是否确认删除Form列编号为"' 
                      + (Array.isArray(Ids) && Ids.length > 2 ? (Ids[0] + '等') : Ids) 
                      + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delColumn(Ids);
        }).then(() => {
          this.getColumnList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      tableExtend(type){
        if (type === 2) {
          if (this.tableDepend === 'table-depend') {
            this.tableMainShow = false;
            this.tableDepend = 'table-extend';
          }else {
            this.tableMainShow = true;
            this.tableDepend ='table-depend';
          }
        }else{
          if (this.tableMain === 'table-main'){
            this.tableDependShow = false;
            this.tableMain ='table-extend';
          }else {
            this.tableDependShow = true;
            this.tableMain ='table-main';
          }
        }
      },
    }
  };
</script>
